package capplauncher.log;

import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;


public class Logger {
	final private String[] EVENTLEVEL = {"INIT","INFO","WARNING","ERROR","UNKNOW"};
	final String LOGFILENAME = "LauncherLog.txt";
	Calendar cal;
	BufferedWriter out;
	
	
	public Logger(String location)throws IOException{
		//location is 
		cal = Calendar.getInstance();
	    out = new BufferedWriter(new FileWriter(location+LOGFILENAME,true));		
	}
	
	
	private String getTime(){
		SimpleDateFormat sdf = new SimpleDateFormat("MM:dd:HH:mm:ss Z");
		return sdf.format(cal.getTime());
	}
	
	public void log(int level,String logLine)throws IOException{
		// Case typo wrong level
		if (level<0||level>3){
			level = 4;
		}	
		//format log String
		String temp = "["+getTime()+" "+EVENTLEVEL[level]+"] "+logLine;
		out.write(temp);
		out.newLine();		
	}
	
	public void logFinish()throws IOException{
		// only call this when close the launcher
		out.close();
	}
	
}
